package com.lxl.testHd.myInputFormat.one;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * @author ：e_lixilin
 * @date ：2022/2/24 9:36
 * @description：
 * @modified By：
 */
public class MyMapReduce extends Configured implements Tool {
    static class MyMapper extends Mapper<Text, Text, Text, Text>{
        //处理输入的文件，并以“key=文件名称，value=文件内容”的形式输出
        @Override
        protected void map(Text key, Text value, Mapper.Context context)
                throws IOException, InterruptedException {
            context.write(key,value);
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = new Configuration() ;
        Job job = Job.getInstance(conf);
        job.setJarByClass(MyMapReduce.class);
        job.setMapperClass(MyMapper.class);
        //指定输入方式为自定义的InputFormat
        job.setInputFormatClass(SelfInputFormat.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        return job.waitForCompletion(true) ? 0:1;
    }

    public static void main(String[] args) throws Exception {
        args = new String[]{"D:\\big-data\\wc", "D:\\big-data\\wc\\outputwcone"};
        ToolRunner.run(  new MyMapReduce(), args) ;
    }
}
